package com.mvvm.ktnet.ui.testViewPager2


import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayoutMediator
import com.mvvm.ktnet.R
import com.mvvm.ktnet.base.myappbase.BaseDbActivity
import com.mvvm.ktnet.base.viewmodel.BaseViewModel
import com.mvvm.ktnet.databinding.AcTestViewpager2Binding

class TestViewPager2 : BaseDbActivity<BaseViewModel, AcTestViewpager2Binding>() {
    override fun layoutId(): Int = R.layout.ac_test_viewpager2


    val fragments by lazy {
        mutableListOf(
            TestViewPager2Fragment(), TestViewPager2Fragment(),
            TestViewPager2Fragment(), TestViewPager2Fragment(),
            TestViewPager2Fragment(), TestViewPager2Fragment(),
            TestViewPager2Fragment(), TestViewPager2Fragment(),
            TestViewPager2Fragment(), TestViewPager2Fragment(),
            TestViewPager2Fragment(), TestViewPager2Fragment()
        )
    }


    val adapter by lazy {
        object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int = 12
            override fun createFragment(position: Int): Fragment = TestViewPager2Fragment()
        }
    }
    var position: Int = 0
    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewPager.apply {
            adapter = this@TestViewPager2.adapter
        }
        TabLayoutMediator(mDatabind.tab, mDatabind.viewPager) { tab, position ->
            tab.text = "测试 ${position + 1}"
        }.attach()

        mDatabind.viewPager.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels)
            }

            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                this@TestViewPager2.position = position
            }

            override fun onPageScrollStateChanged(state: Int) {
                super.onPageScrollStateChanged(state)
            }
        })

        if (position in fragments.indices) (fragments[position] as? TestViewPager2Fragment)?.refresh()

    }



    override fun createObserver() {

    }

    override fun initData() {

    }
}